package com.tpfinal.agenda.model.factories;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import com.tpfinal.agenda.core.mysqlrequests.MySQL;
import com.tpfinal.agenda.model.City;

public class CitiesFactory {

	private static HashMap<String, City> cache = new HashMap<String, City>();

	public static final ArrayList<City> createCityListFromMySqlResult(
			ResultSet data) throws SQLException {

		ArrayList<City> list = new ArrayList<City>();

		while (data.next()) {

			list.add(createCity(data));

		}

		return list;

	}

	public static City createCity(ResultSet data) throws SQLException {

		City c = cache.get(data.getString(MySQL.MYSQL_COLUMN_CP));

		if (c == null) {

			int zip = data.getInt(MySQL.MYSQL_COLUMN_CP);
			String cityName = data.getString(MySQL.MYSQL_COLUMN_LOCALIDAD);
			String state = data.getString(MySQL.MYSQL_COLUMN_PROVINCIA);
			int ddn = data.getInt(MySQL.MYSQL_COLUMN_DDN);

			c = new City(zip, cityName, state, ddn);

			cache.put(data.getString(MySQL.MYSQL_COLUMN_CP), c);

		}

		return c;

	}
}
